<?php
class Weixinapi extends Base_Controller
{
	private $_appid;
	private $_appsecret;
	private $_wx_type;
//	private $_backapi_token;
//	private $_backapi_aeskey;

	public function __construct()
	{
		parent::__construct();
	}

	private function getSourceConfig($source = ''){
        $this->config->load('weixin');
        if (!empty($source)){
            $this->_appid = $this->config->item($source.'_appid');
            $this->_appsecret = $this->config->item($source.'_appsecret');
            $this->_wx_type = $this->config->item($source.'_type');
            //加密模式
            $this->_backapi_token = $this->config->item($source.'_backapi_token');
            $this->_backapi_aeskey = $this->config->item($source.'_backapi_aeskey');
        }else{
            //默认能量逗服务号
            $this->_appid = $this->config->item('nldou_fuwu_appid');
            $this->_appsecret = $this->config->item('nldou_fuwu_appsecret');
            $this->_wx_type = $this->config->item('nldou_fuwu_type');
            //加密模式
            $this->_backapi_token = $this->config->item($source.'_backapi_token');
            $this->_backapi_aeskey = $this->config->item($source.'_backapi_aeskey');
        }
    }

    //被动回复消息
    /**
     * @param string $source 公众号类型
     *
     */
    public function response_msg($source = '')
    {
        $this->getSourceConfig($source);
        $this->load->library('wxinterface/Wxcallbackapi',array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>$this->_wx_type,
            "token"=>$this->_backapi_token,"aeskey"=>$this->_backapi_aeskey));

        if (!isset($_GET['echostr'])) {
            $result=$this->wxcallbackapi->responseMsg();
            echo $result;
            exit;
        }else{
            $this->wxcallbackapi->valid();
        }
    }
    public function getWxQrcode($source = '')
    {
        $this->getSourceConfig($source);
        $this->load->library('wxinterface/Wxinterface',array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>$this->_wx_type));
        $res = $this->wxinterface->getWxQrcode("QR_STR_SCENE","sls.falnfa.postid.12345678");
        var_dump($res);
    }

    public function getCurrentAutoreplayInfo($source = '')
    {
        $this->getSourceConfig($source);
        $this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>$this->_wx_type));
        $res=$this->wxinterface->getCurrentAutoreplayInfo();
        echo '<pre>';
        var_dump($res);
        echo '</pre>';
    }
	
	//查询菜单设置
    public function get_menu_info($source = '')
    {
        $this->getSourceConfig($source);
        $this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>$this->_wx_type));
    	$res=$this->wxinterface->get_menu_info();
    	$this->success($res);
    }
    
    //设置菜单
    public function set_menu($source = '')
    {
        $this->getSourceConfig($source);
        $this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>$this->_wx_type));
        $button=array('button'=>array(
            'name'=>'能量空间',
            'sub_button'=>array(
                array('type'=>'view','name'=>'卡牌玩法视频教程','url'=>'http://wap.nldou.com/hardfun/'),
                array('type'=>'view','name'=>'文章精选','url'=>'http://mp.weixin.qq.com/mp/homepage?__biz=MzA3MTQ5ODAxNA==&hid=1&sn=91bbb626c0aa717ce1848c38baadfce7&scene=18#wechat_redirect')
            )
        ));
    	$button='{"button":['.
            '{"name":"能量逗","sub_button":['.
            '{"type":"click","name":"加入我们","key":"V1_JOIN_US"},'.
            '{"type":"click","name":"关于我们","key":"V1_ABOUT_US"},'.
    	    '{"type":"view","name":"精彩回顾","url":"http://mp.weixin.qq.com/mp/homepage?__biz=MzA3MTQ5ODAxNA==&hid=1&sn=91bbb626c0aa717ce1848c38baadfce7#wechat_redirect"},'.
    	    '{"type":"view","name":"卡牌规则","url":"http://wap.nldou.com/hardfun"}]},'.
		    '{"type":"view","name":"卡牌规则","url":"http://wap.nldou.com/hardfun"},'.
		    '{"name":"商城","sub_button":['.
    	    '{"type":"view","name":"成为代言人","url":"https://h5.koudaitong.com/v2/trade/directseller/tutorial.html?kdt_id=18168297"},'.
    		'{"type":"view","name":"推广海报","url":"https://wap.koudaitong.com/salesman/poster/poster/index?kdt_id=18168297"},'.
            '{"type":"view","name":"能量商城","url":"https://h5.koudaitong.com/v2/feature/1ekqipm0r"}]}]}';
    	$res=$this->wxinterface->set_menu($button);
    	$this->success($res);
    }
    
    //js sdk初始化验证
    public function init_wx_jsapi()
    {
        $get = $this->input->get();
        $source = isset($get['source'])?$get['source']:'';
    	if(isset($get['fromUrl'])) $fromUrl = $get['fromUrl'];
        else $this->error('lack fromUrl');

    	$this->getSourceConfig($source);
        $this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>$this->_wx_type));
    	//签名
    	$init['signature'] = $this->wxinterface->getJsapiSignature($fromUrl);
    	//签名使用的随机串
    	$init['nonceStr'] = $this->wxinterface->getJsapiNonceStr();
    	//签名使用的时间戳
    	$init['timestamp'] = $this->wxinterface->getJsapiTimestamp();
    	 
    	$init['appid'] = $this->wxinterface->getAppid();
    	 
    	$this->success($init);
    }

    public function longurl2short()
    {
        $url = $this->input->get('url');
//        $source = isset($get['source'])?$get['source']:'';
//        if(!isset($get['url'])||empty($get['url'])) $this->error();
        $this->getSourceConfig('');
        $this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>$this->_wx_type));
//        $url = "https://h5.youzan.com/v2/trade/directsellerJump/jump?kdt_id=18168297&sl=2VSkUa&redirect_uri=";
//        $url .= urlencode('https://h5.youzan.com/v2/feature/JLVR0uWTtc?sls=2VSkUa');
        // $url = urldecode($rawurl);
        $res = $this->wxinterface->longurl2short($url);
//        $url2 = "https://wap.youzan.com/salesman/home/tutorial/index?kdt_id=18168297";
//        $res2 = $this->wxinterface->longurl2short($url2);
        echo '<pre>';
        var_dump($res);
        echo '</pre>';
//        echo '<pre>';
//        var_dump($res2);
//        echo '</pre>';
    }

    public function getUserInfo($openid)
    {
        $this->getSourceConfig('');
        $this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>$this->_wx_type));
        $res=$this->wxinterface->getUserInfo($openid);
        echo '<pre>';
        var_dump($res);
        echo '</pre>';
    }
    

    //发送客服消息
    public function send_custom_msg($source = '', $data = ''){
	    if (!empty($data)){
	        $this->getSourceConfig($source);
            $this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>$this->_wx_type));
            $res=$this->wxinterface->send_custom_msg($data);
            var_dump($res);
        }else{
	        $this->error("参数不全");
        }
    }
    
    
}